package com.gui.components.indexTabel;

import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JDialog;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import com.constants.PlaceOfDiviness;
import com.dataModel.Quran;
import com.panels.MainPanel;

public class IndexTable extends JTable implements MouseListener, KeyListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -99864382917397029L;
	JDialog indexDialog;

	public IndexTable(JDialog indexDialog) {
		super(114, 4);
		this.setModel(new IndexTableModel(114, 4));
		this.setRowHeight(28);
		TableCellRenderer tableCellRenderer = new IndexTableCellRenderer();
		for (int i = 0; i < this.getColumnCount(); i++) {
			this.getColumnModel().getColumn(i).setCellRenderer(
					tableCellRenderer);
		}
		this.indexDialog = indexDialog;
		this.addMouseListener(this);
		int fontStyle = this.getFont().getStyle();
		String fontName = this.getFont().getName();
		Font font = new Font(fontName, Font.BOLD, 17);
		this.setFont(font);
		this.getColumnModel().getColumn(0).setHeaderValue("Number");
		this.getColumnModel().getColumn(1).setHeaderValue("Sourah");
		this.getColumnModel().getColumn(2).setHeaderValue("Page Number");
		this.getColumnModel().getColumn(3).setHeaderValue("Place Of revliance");

		this.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		for (int i = 0; i < 114; i++) {
			this.getModel().setValueAt(i + 1, i, 0);
			this.getModel().setValueAt(Quran.getSowar()[i].getName(), i, 1);
			this.getModel().setValueAt(Quran.getSowar()[i].getPageNumber(), i,
					2);
			String placeOfDivinessName = (Quran.getSowar()[i]
					.getPlaceOfDiviness() == PlaceOfDiviness.MAKKAH) ? Quran
					.getSowar()[i].getPlaceOfDiviness().makahName : Quran
					.getSowar()[i].getPlaceOfDiviness().madinahName;
			this.getModel().setValueAt(
					Quran.getSowar()[i].getPlaceOfDiviness(), i, 3);

		}

	}

	public void mouseClicked(MouseEvent e) {

		int rowIndex = this.rowAtPoint(e.getPoint());

		this.setRowSelectionInterval(rowIndex, rowIndex);
		if (e.getClickCount() % 2 == 0) {
			displaySourah(rowIndex);
			System.out.println(rowIndex);
		}

	}

	private void displaySourah(int rowIndex) {
		int pageNumber = Integer.parseInt(this.getModel().getValueAt(rowIndex,
				2).toString());

		MainPanel.getMainPanel().getPagesPanel().displayPage(pageNumber);
		this.indexDialog.dispose();
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

}
